﻿(*                  FnSharp              

   Base class library extensions and frameworks aimed 
   specifically at improving F# developers lives!

   Written by: Huw Simpson
   License: Microsoft Public License (Ms-PL) 
   Available at: http://fnsharp.codeplex.com

*)
namespace FnSharp.Bcl.Core

    open System
    open Atom

    [<RequireQualifiedAccess>]
    [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
    module Memoization =
        /// Memoize the function.
        let create f = //Taken from http://fssnip.net/2r, ms-pl license (Nick Palladinos)
            let cacheAtom = atom Map.empty
            fun x ->
                match (!cacheAtom).TryFind(x) with
                | Some res -> res
                | None ->
                        let res = f x
                        switch cacheAtom (fun cache -> cache.Add(x,res))
                        res